﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Drive.DbAccess
{
    public class JdbcString
    {
        public string Prefix { get; set; }
        public string Driver { get; set; }
        public string Name { get; private set; }
        public string Host { get; set; }
        public int Port { get; set; }
        public string Database { get; set; }
        public string Password { get; set; }
        public string UserName { get; set; }

        public static JdbcString Parse(String jdbc)
        {
            var items = jdbc.Split(new char[] { ':' }, 3, StringSplitOptions.RemoveEmptyEntries);
            var jdbcs = new JdbcString();
            jdbcs.Prefix = items[0];
            jdbcs.Driver = items[1];
            jdbcs.Name = jdbc;
            var p = items[2].Substring(2).Split(new char[] { '/' }, 2);
            var h = p[0].Split(':');
            if (h.Length == 1)
            {
                jdbcs.Host = h[0];
            }
            else if (h.Length == 2)
            {
                jdbcs.Host = h[0];
                jdbcs.Port = int.Parse(h[1]);
            }
            jdbcs.Database = p[1];
            return jdbcs;
        }

        public IDbInterface GetDbInterface()
        {
            switch (this.Driver)
            {
                case "mysql":
                    return new MySqlDb(this, this.Name);
                case "postgresql":
                    return new PostgreDb(this, this.Name);
                default:
                    break;
            }
            throw new NotImplementedException();
        }
    }
}
